﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="FriendlyUrlRouteHandler.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using InnovaApp.Portals.MVC4Portal.Models.MetaModels;
using INNOVACALL.Framework.Common.Logging;

namespace InnovaApp.Portals.MVC4Portal.Controllers.FriendlyURLHandling
{
    /// <summary>
    /// Class FriendlyUrlRouteHandler
    /// </summary>
    public class FriendlyUrlRouteHandler : _FriendlyURLHandlingBase
    {

        /// <summary>
        /// Returns the HTTP handler by using the specified HTTP context.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <returns>The HTTP handler.</returns>
        protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
        {
            var friendlyUrl = (string)requestContext.RouteData.Values["FriendlyUrl"];
            if (string.IsNullOrEmpty(friendlyUrl))
            {
                friendlyUrl = "/";
            }
            ContentAccessor_MetaModel contentAccessor = friendlyUrl.GetRouteData();
            requestContext.RouteData.Values["friendlyUrl"] = contentAccessor.FriendlyUrl;
            requestContext.RouteData.Values["controller"] = contentAccessor.Controller;
            requestContext.RouteData.Values["action"] = contentAccessor.Action;
            requestContext.RouteData.Values["language"] = contentAccessor.Language;
			requestContext.RouteData.Values["layoutMenuId"] = contentAccessor.LayoutMenuId;
            requestContext.RouteData.Values["structurePageId"] = contentAccessor.StructurePageId;
            requestContext.RouteData.Values["openingMessageResourceTag"] = contentAccessor.OpeningMessageResourceTag;
            requestContext.RouteData.Values["openingMessageResourceStringFormatParameters"] = contentAccessor.OpeningMessageResourceStringFormatParameters;
            //Réécriture de l'url pour qu'elle s'affiche telle que demandée
            requestContext.HttpContext.RewritePath(contentAccessor.FriendlyUrl);
            return base.GetHttpHandler(requestContext);
        }
    }
}
